<?php 
Class sysController extends appController
{
	/**
	 * 显示网站设置页面
	 */
	public function website()
	{
		$tpl_list = $this->getTpl();
		$setting = Model('sys')->getSetting();
		$this->assign('tpl_list',$tpl_list);
		$this->assign('setting',$setting);
		$this->display('website.html');
	}
	
	/**
	 * 存储网站设置
	 */
	public function saveSetting()
	{
		$this->admin_priv($_GET['type'], 2);
		$info = $_POST;
		$result = array('error' => 1,'content' => '');
		if(Model('sys')->saveSetting($info))
		{
			$result['error'] = 0;
			$result['content'] = Lang('VALID_OPERATION');
		}
		else
		{
			$result['content'] = Lang('INVALID_OPERATION');
		}
		$this->showMsg($result);
	}
	
	/**
	 * 获取邮箱设置信息
	 */
	public function smtp()
	{
		$setting = Model('sys')->getSetting();
		$this->assign('setting',$setting);
		$this->display('smtp.html');
	}
	private function make_index()
	{
		$static = new StaticPage;
		$time=60;
		$saveDir=ROOT_PATH;
		$activePage="http://".$_SERVER['HTTP_HOST']."/index.php";
		
		$fileName=$static->getPageName(basename($activePage),'html');
		$flag=$static->reMakeCondition($saveDir.'/'.$fileName,$time);
		if(!$flag)
		{
			
		}
		$static->makeStaticPage($activePage,'html',$saveDir);
		
	}
	
	/**
	 * 显示备份的数据库文件
	 */
	public function db()
	{
		$sql_list = Model('sys')->get_sql_list();
		$this->assign('sql_list',$sql_list);
		$this->display('database.html');
	}
	
	/**
	 * 删除已备份的数据库文件
	 */
	public function delsql()
	{
		$file_name = empty($_GET['file_name']) ? '': trim($_GET['file_name']);
		$filename=TEMP_PATH. '/sqldata/'.$file_name;
		$result=array('error'=>1,'content'=>Lang('INVALID_OPERATION'));
		if(is_file($filename))
		{
			@unlink($filename);
			$result['error']=0;
			$result['content']=Lang('VALID_OPERATION');
		}
		$this->showMsg($result);
	}
	public function restore()
	{
		$file_name = empty($_GET['file_name']) ? '': trim($_GET['file_name']);
		$result=Model('sys')->import($file_name);
		$this->showMsg($result);
	}
	
	/**
	 * 生成备份的数据库文件
	 */
	public function backup()
	{
		$result = Model('sys')->backup();
		$this->showMsg($result);
	}
	
	public function testemail()
	{
		$smtp=$_POST['smtp'];
		$smtp_username=$_POST['smtp_username'];
		$smtp_password=$_POST['smtp_password'];
		$smtp_email=$_POST['smtp_email'];
		$test_email=$_POST['test_email'];
		$content='<p>Hello,欢迎使用Qiaqia CMS</p>';
		$result=array('error'=>1,'content'=>'发送失败');
	
		if($test_email==''||hy_check($test_email,'e')=='#@_error')
		{
			$result['content']='测试地址格式不正确';
			$this->showMsg($result);
		}
	
		$mail=new PHPMailer();
		$mail->IsSMTP();                                      // Set mailer to use SMTP
		$mail->Host = $smtp;  // Specify main and backup server
		$mail->SMTPAuth = true;                               // Enable SMTP authentication
		$mail->Username = $smtp_username;                            // SMTP username
		$mail->Password = $smtp_password;                           // SMTP password
	
		$mail->From = $smtp_email;
		$mail->FromName = '维赛网络科技有限公司';
	
		$mail->AddAddress($test_email, 'Hungry');  // Add a recipient
		//$mail->AddAddress('990248639@qq.com', 'shawn');  // Add a recipient
		$mail->WordWrap = 50;                                 // Set word wrap to 50 characters
	
		$mail->Subject = 'Qiaqia CMS 测试邮件';
		$mail->Body    = $content;
		$mail->AltBody = $content;
		$res=$mail->Send();
	
		if($res)
		{
			$result['error']=0;
			$result['content']='发送成功';
	
		}
		$this->showMsg($result);
	}
	
	private function writeStatic()
	{
		
	}
	
	/**
	 * 获取模板
	 * @return string
	 */
	private function getTpl()
	{
		$path=HOME_PATH.'/View';
		$real_list = array();
		$folder = opendir($path);
		
		while ($file = readdir($folder))
		{
		
			if($file == '.' || $file == '..')
			{
				continue;
			}elseif(is_dir($path.'/'.$file))
			{
				$tpl_list[] = $file;
			}
		}
		
		return $tpl_list;
	}
	
}